package org.maruero.ejb.impl;

import java.util.List;

import javax.ejb.Stateful;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import javax.persistence.PersistenceContextType;
import javax.persistence.Query;

import org.maruero.ejb.entity.Book;
import org.maruero.ejb.inter.Books;

@Stateful( name = "Books")
public class BooksImpl implements Books {

    @PersistenceContext(unitName = "book-unit", type = PersistenceContextType.EXTENDED)
    private EntityManager entityManager;

    public void addBook(Book book) throws Exception {
        entityManager.persist(book);
    }

    public void deleteBook(Book book) throws Exception {
        entityManager.remove(book);
    }

    public List<Book> getBooks() throws Exception {
        Query query = entityManager.createQuery("SELECT b from Book as b");
        return query.getResultList();
    }
    
    public void load() throws Exception{
    	addBook(new Book( "Sharpe" , "Bernard Cornwell" , 2006 ));
		addBook(new Book( "The Lord Of Rings" , "J. R. R. Tolkien" , 1944 ));
		addBook(new Book( "The Grail Quest" , "BernardCornwell" , 2000 ));
		addBook(new Book( "Sherlock Holmes" , "Conan Doyle" , 1887 ));	
    }
}
